package com.yuhanginfo.modules.web.web;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yuhanginfo.common.web.BaseController;

/**
 * 文件管理
 * @author yinhao
 * @version 2017-09-11
 */
@Controller
@RequestMapping(value = "${frontPath}/web/file/")
public class WebFileController extends BaseController {

	@RequestMapping(value = "getFile")
	public void getFile(String filePath, HttpServletResponse response) {
		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		try {
		    File file = new File(filePath);
		    if(!file.exists()){
		    	return;
		    }
		    byte[] b = new byte[1024*1024];
		    bos = new BufferedOutputStream(response.getOutputStream());
		    fis = new FileInputStream(file);
		    int length = -1;
		    while((length = fis.read(b, 0, b.length)) != -1){
		    	bos.write(b, 0, length);
		    }
		} catch (Exception ex) {
			logger.error(ex.getMessage(),ex);
		}finally{
			if(bos != null){
				try {
					bos.close();
				} catch (IOException e) {
					logger.error(e.getMessage(),e);
				}
			}
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					logger.error(e.getMessage(),e);
				}
			}
		}
	}

}